<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>排序和查找算法的使用</title>
    <meta name="description" content="TBOX提供了各种常用算法，对容器中的元素进行各种操作，这里主要介绍下排序和查找算法。排序算法目前支持如下几种：  快速排序：tb_quick_sort  堆排序：  tb_heap_sort  插入排序：tb_insert_sort  冒泡排序：tb_bubble_sort并且提供通用的tb_sort接口，对各...">

    
    <meta name="keywords" content="tbox,算法,排序,查找,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2016/02/04/algorithm-sort-find/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>排序和查找算法的使用</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2016-02-04
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#tbox" title="Category: tbox" rel="category">tbox</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a-->
        <a href="/cn/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a>&nbsp;
    
        <!--a href="/tag/#%E7%AE%97%E6%B3%95" title="Tag: 算法" rel="tag">算法</a-->
        <a href="/cn/tag/#算法" title="Tag: 算法" rel="tag">算法</a>&nbsp;
    
        <!--a href="/tag/#%E6%8E%92%E5%BA%8F" title="Tag: 排序" rel="tag">排序</a-->
        <a href="/cn/tag/#排序" title="Tag: 排序" rel="tag">排序</a>&nbsp;
    
        <!--a href="/tag/#%E6%9F%A5%E6%89%BE" title="Tag: 查找" rel="tag">查找</a-->
        <a href="/cn/tag/#查找" title="Tag: 查找" rel="tag">查找</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>TBOX提供了各种常用算法，对容器中的元素进行各种操作，这里主要介绍下排序和查找算法。</p>

<p>排序算法目前支持如下几种：</p>

<ol>
  <li>快速排序：<code class="highlighter-rouge">tb_quick_sort</code></li>
  <li>堆排序：  <code class="highlighter-rouge">tb_heap_sort</code></li>
  <li>插入排序：<code class="highlighter-rouge">tb_insert_sort</code></li>
  <li>冒泡排序：<code class="highlighter-rouge">tb_bubble_sort</code></li>
</ol>

<p>并且提供通用的<code class="highlighter-rouge">tb_sort</code>接口，对各种排序算法进行自动适配，使得任何情况下，性能都是最优的。
例如：</p>

<ol>
  <li>对具有随机迭代特性的容器，采用快速排序来优化</li>
  <li>对具有随机迭代特性，并且是超大规模的容器，采用堆排序</li>
  <li>对只能线性迭代的容器采用冒泡排序</li>
</ol>

<p>所以一般情况下，只需要调用<code class="highlighter-rouge">tb_sort</code>就行了</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 初始化一个vector，元素类型为tb_long_t， 满256个元素自动增长
</span>    <span class="n">tb_vector_ref_t</span> <span class="n">vector</span> <span class="o">=</span> <span class="n">tb_vector_init</span><span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="n">tb_item_func_long</span><span class="p">());</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">vector</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 插入一些元素
</span>        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">10</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">2</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">5</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">6</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">9</span><span class="p">);</span>
       
        <span class="c1">// 排序所有，第二个参数是比较器函数，默认使用容器内置的比较器
</span>        <span class="n">tb_sort_all</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">);</span>

        <span class="c1">// 释放vector
</span>        <span class="n">tb_vector_exit</span><span class="p">(</span><span class="n">vector</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

<p>对于查找算法，目前提供：</p>

<ol>
  <li>线性查找：    tb_find</li>
  <li>反向线性查找：tb_rfind</li>
  <li>二分法查找：  tb_binary_find</li>
</ol>

<p>如果容器具有随机迭代特性，你就可以使用二分查找来优化，例如：vector、原生数组等等。。</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 初始化一个vector，元素类型为tb_long_t， 满256个元素自动增长
</span>    <span class="n">tb_vector_ref_t</span> <span class="n">vector</span> <span class="o">=</span> <span class="n">tb_vector_init</span><span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="n">tb_item_func_long</span><span class="p">());</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">vector</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 插入一些有序元素
</span>        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">1</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">2</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">4</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">6</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">9</span><span class="p">);</span>
       
        <span class="c1">// 使用二分查找法，快速查找元素，算法复杂度O(log2)
</span>        <span class="n">tb_size_t</span> <span class="n">itor</span> <span class="o">=</span> <span class="n">tb_binary_find_all</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">5</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">itor</span> <span class="o">!=</span> <span class="n">tb_iterator_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="c1">// 获取元素值：5
</span>            <span class="n">tb_size_t</span> <span class="n">value</span> <span class="o">=</span> <span class="n">tb_iterator_item</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="n">itor</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="c1">// 释放vector
</span>        <span class="n">tb_vector_exit</span><span class="p">(</span><span class="n">vector</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

<p>你也可以指定比较器函数，来更灵活的进行查找。</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 按降序比较函数
</span>    <span class="k">static</span> <span class="n">tb_long_t</span> <span class="nf">your_comp_func</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">ltem</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">rtem</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="p">((</span><span class="n">tb_long_t</span><span class="p">)</span><span class="n">ltem</span> <span class="o">&lt;</span> <span class="p">(</span><span class="n">tb_long_t</span><span class="p">)</span><span class="n">rtem</span><span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="p">((</span><span class="n">tb_long_t</span><span class="p">)</span><span class="n">ltem</span> <span class="o">&gt;</span> <span class="p">(</span><span class="n">tb_long_t</span><span class="p">)</span><span class="n">rtem</span><span class="o">?</span> <span class="o">-</span><span class="mi">1</span> <span class="o">:</span> <span class="mi">0</span><span class="p">));</span>
    <span class="p">}</span>

    <span class="c1">// 初始化一个vector，元素类型为tb_long_t， 满256个元素自动增长
</span>    <span class="n">tb_vector_ref_t</span> <span class="n">vector</span> <span class="o">=</span> <span class="n">tb_vector_init</span><span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="n">tb_item_func_long</span><span class="p">());</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">vector</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 插入一些有序元素
</span>        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">1</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">2</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">4</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">6</span><span class="p">);</span>
        <span class="n">tb_vector_insert_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">9</span><span class="p">);</span>
       
        <span class="c1">// 使用二分查找法，快速查找元素，并且指定自己的比较器函数
</span>        <span class="n">tb_size_t</span> <span class="n">itor</span> <span class="o">=</span> <span class="n">tb_binary_find_all_if</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="n">your_comp_func</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">itor</span> <span class="o">!=</span> <span class="n">tb_iterator_tail</span><span class="p">(</span><span class="n">vector</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="c1">// 获取元素值：5
</span>            <span class="n">tb_size_t</span> <span class="n">value</span> <span class="o">=</span> <span class="n">tb_iterator_item</span><span class="p">(</span><span class="n">vector</span><span class="p">,</span> <span class="n">itor</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="c1">// 释放vector
</span>        <span class="n">tb_vector_exit</span><span class="p">(</span><span class="n">vector</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

<p>原生的数组也是可以使用算法进行比较的，下面给个比较常用的查找例子：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 定义一个字符集操作的数据结构
</span>    <span class="k">typedef</span> <span class="k">struct</span> <span class="n">__tb_charset_t</span>
    <span class="p">{</span>
        <span class="n">tb_size_t</span>           <span class="n">type</span><span class="p">;</span>
        <span class="n">tb_char_t</span> <span class="k">const</span><span class="o">*</span>    <span class="n">name</span><span class="p">;</span>
        <span class="n">tb_long_t</span>           <span class="p">(</span><span class="o">*</span><span class="n">get</span><span class="p">)(</span><span class="n">tb_static_stream_ref_t</span> <span class="n">sstream</span><span class="p">,</span> <span class="n">tb_bool_t</span> <span class="n">be</span><span class="p">,</span> <span class="n">tb_uint32_t</span><span class="o">*</span> <span class="n">ch</span><span class="p">);</span>
        <span class="n">tb_long_t</span>           <span class="p">(</span><span class="o">*</span><span class="n">set</span><span class="p">)(</span><span class="n">tb_static_stream_ref_t</span> <span class="n">sstream</span><span class="p">,</span> <span class="n">tb_bool_t</span> <span class="n">be</span><span class="p">,</span> <span class="n">tb_uint32_t</span> <span class="n">ch</span><span class="p">);</span>

    <span class="p">}</span><span class="n">tb_charset_t</span><span class="p">;</span>

    <span class="c1">// 定义一个原生数组
</span>    <span class="k">static</span> <span class="n">tb_charset_t</span> <span class="n">charsets</span><span class="p">[]</span> <span class="o">=</span>
    <span class="p">{</span>
        <span class="p">{</span><span class="n">TB_CHARSET_TYPE_ASCII</span><span class="p">,</span>     <span class="s">"ascii"</span><span class="p">,</span>    <span class="n">tb_charset_ascii_get</span><span class="p">,</span>   <span class="n">tb_charset_ascii_set</span>    <span class="p">}</span>
    <span class="p">,</span>   <span class="p">{</span><span class="n">TB_CHARSET_TYPE_GB2312</span><span class="p">,</span>    <span class="s">"gb2312"</span><span class="p">,</span>   <span class="n">tb_charset_gb2312_get</span><span class="p">,</span>  <span class="n">tb_charset_gb2312_set</span>   <span class="p">}</span>
    <span class="p">,</span>   <span class="p">{</span><span class="n">TB_CHARSET_TYPE_GBK</span><span class="p">,</span>       <span class="s">"gbk"</span><span class="p">,</span>      <span class="n">tb_charset_gb2312_get</span><span class="p">,</span>  <span class="n">tb_charset_gb2312_set</span>   <span class="p">}</span>
    <span class="p">,</span>   <span class="p">{</span><span class="n">TB_CHARSET_TYPE_ISO8859</span><span class="p">,</span>   <span class="s">"iso8859"</span><span class="p">,</span>  <span class="n">tb_charset_iso8859_get</span><span class="p">,</span> <span class="n">tb_charset_iso8859_set</span>  <span class="p">}</span>
    <span class="p">,</span>   <span class="p">{</span><span class="n">TB_CHARSET_TYPE_UCS2</span><span class="p">,</span>      <span class="s">"ucs3"</span><span class="p">,</span>     <span class="n">tb_charset_ucs2_get</span><span class="p">,</span>    <span class="n">tb_charset_ucs2_set</span>     <span class="p">}</span>
    <span class="p">,</span>   <span class="p">{</span><span class="n">TB_CHARSET_TYPE_UCS4</span><span class="p">,</span>      <span class="s">"ucs4"</span><span class="p">,</span>     <span class="n">tb_charset_ucs4_get</span><span class="p">,</span>    <span class="n">tb_charset_ucs4_set</span>     <span class="p">}</span>
    <span class="p">,</span>   <span class="p">{</span><span class="n">TB_CHARSET_TYPE_UTF16</span><span class="p">,</span>     <span class="s">"utf16"</span><span class="p">,</span>    <span class="n">tb_charset_utf16_get</span><span class="p">,</span>   <span class="n">tb_charset_utf16_set</span>    <span class="p">}</span>
    <span class="p">,</span>   <span class="p">{</span><span class="n">TB_CHARSET_TYPE_UTF32</span><span class="p">,</span>     <span class="s">"utf32"</span><span class="p">,</span>    <span class="n">tb_charset_utf32_get</span><span class="p">,</span>   <span class="n">tb_charset_utf32_set</span>    <span class="p">}</span>
    <span class="p">,</span>   <span class="p">{</span><span class="n">TB_CHARSET_TYPE_UTF8</span><span class="p">,</span>      <span class="s">"utf8"</span><span class="p">,</span>     <span class="n">tb_charset_utf8_get</span><span class="p">,</span>    <span class="n">tb_charset_utf8_set</span>     <span class="p">}</span>
    <span class="p">};</span>

    <span class="c1">// 按名字查找比较函数
</span>    <span class="k">static</span> <span class="n">tb_long_t</span> <span class="nf">tb_charset_comp_by_name</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">item</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">name</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">tb_stricmp</span><span class="p">(((</span><span class="n">tb_charset_ref_t</span><span class="p">)</span><span class="n">item</span><span class="p">)</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_char_t</span> <span class="k">const</span><span class="o">*</span><span class="p">)</span><span class="n">name</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="c1">// 将原生的数组，初始化成一个迭代器
</span>    <span class="n">tb_iterator_t</span> <span class="n">iterator</span> <span class="o">=</span> <span class="n">tb_iterator_init_mem</span><span class="p">(</span><span class="n">charsets</span><span class="p">,</span> <span class="n">tb_arrayn</span><span class="p">(</span><span class="n">charsets</span><span class="p">),</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">tb_charset_t</span><span class="p">));</span>

    <span class="c1">// 针对这个迭代器根据名字进行二分法查找
</span>    <span class="n">tb_size_t</span> <span class="n">itor</span> <span class="o">=</span> <span class="n">tb_binary_find_all_if</span><span class="p">(</span><span class="o">&amp;</span><span class="n">iterator</span><span class="p">,</span> <span class="n">tb_charset_comp_by_name</span><span class="p">,</span> <span class="s">"utf8"</span><span class="p">);</span>

    <span class="c1">// 如果找到
</span>    <span class="k">if</span> <span class="p">(</span><span class="n">itor</span> <span class="o">!=</span> <span class="n">tb_iterator_tail</span><span class="p">(</span><span class="o">&amp;</span><span class="n">iterator</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="c1">// 获取元素对象
</span>        <span class="n">tb_charset_t</span><span class="o">*</span> <span class="n">charset</span> <span class="o">=</span> <span class="p">(</span><span class="n">tb_charset_t</span><span class="o">*</span><span class="p">)</span><span class="n">tb_iterator_item</span><span class="p">(</span><span class="o">&amp;</span><span class="n">iterator</span><span class="p">,</span> <span class="n">itor</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

<p>注：上面的例子摘录自TBOX库内部的代码，仅供参考，不能直接copy使用。</p>


        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        

        
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2016/02/03/project-description/">xmake工程描述入门</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2016/02/04/container-hash/">哈希容器的使用</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
